package es.deusto.pfc.garuda;

import es.deusto.pfc.garuda.server.ClientManager;
import es.deusto.pfc.garuda.server.DAO;
import es.deusto.pfc.garuda.server.net.Net;

public class GarudaServer
{
	public GarudaServer()
	{
		super();
		Config.load("");
		final Net net = Net.getInstance
		(
			new ClientManager
			(
				new DAO
				(
					Config.dbdriver, Config.dburl, Config.dbuser, Config.dbpasswd
				)
			)
		);
		Runtime.getRuntime().addShutdownHook(new Thread()
		{
			@Override
			public void run()
			{
				GarudaServer.logState(this, "starting shutdown..");
				net.gracefullyStop();
				Config.save();
				GarudaServer.logState(this, "shutdown ended");
			}
		});
		GarudaServer.logState(this, "up and running");
		net.start();
	}
	
	public static void logState(Object sender, String state)
	{
		System.out.println("*** " + sender.getClass().getSimpleName() + " " + state+ ". ***");
	}

	public static void main(String[] args)
	{
		GarudaServer.logState(GarudaServer.class, "starting...");
		new GarudaServer();
	}
}
